package app.widget.dialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import app.utils.CommonUtils;
import app.widget.BaseDialog;

import com.longevityassistant.activity.R;

/**
 * 
 * @author EX-LUOCHUN001
 *
 */
public class MultiSelectDialogEx extends BaseDialog {
	/**
	 * 选项集合
	 */
	private List<SelectItemBean> mDataList = new ArrayList<SelectItemBean>();
	/**
	 * 显示的Key值
	 */
	public final String mShowKey;
	/**
	 * 最多选多少个选项
	 */
	public int mMaxSelectItem;
	/**
	 * context
	 */
	private Context mContext;
	/**
	 * adapter
	 */
	private DialogAdapter mAdapter;
	/**
	 * 监听
	 */
	private OnMultiSelectListener mOnMultiSelectListener;
	
	private List<Boolean> mTempDataList = null;
	/**
	 * 构造函数
	 * @param context	context
	 * @param title		标题
	 * @param dataList  数据源
	 * @param showKey	显示的key值
	 */
	public MultiSelectDialogEx(Context context,String title,List<Map<String, Object>> dataList,String showKey) {
		super(context);
		mShowKey = showKey;
		mContext = context;
		setTitle(title);
		if(dataList!=null){
			mMaxSelectItem = dataList.size();
			for(int i=0;i<dataList.size();i++){
				mDataList.add(new SelectItemBean(dataList.get(i)));
			}
		}
		Log.v("luochun", "MultiSelectDialogEx1");
		
		
		
	}
	/**
	 * 构造函数
	 * @param context		context
	 * @param title			标题
	 * @param dataList		数据源
	 * @param showKey		显示的key值
	 * @param maxSelectItem	最多选多少个选项
	 */
	public MultiSelectDialogEx(Context context,String title,List<Map<String, Object>> dataList,String showKey,int maxSelectItem) {
		super(context);
		mShowKey = showKey;
		mContext = context;
		mMaxSelectItem = maxSelectItem;
		setTitle(title);
		if(dataList!=null){
			for(int i=0;i<dataList.size();i++){
				mDataList.add(new SelectItemBean(dataList.get(i)));
			}
		}
		Log.v("luochun", "MultiSelectDialogEx2");
	}
	/**
	 * 获取当前已经选了多少个选项
	 * @return	选中个数
	 */
	private int getSelectCount(){
		int count=0;
		if(mDataList!=null){
			
			for(int i=0;i<mDataList.size();i++){
				if(mDataList.get(i).isChecked){
					count++;
				}
			}
		}
		return count;
	}
	/**
	 * 获取当前已经被选中的数据
	 * @return	被选中的数据列表
	 */
	private List<Map<String, Object>> getSelectMapList(){
		List<Map<String, Object>> selectMapList = new ArrayList<Map<String,Object>>();
		if(mDataList!=null){
			
			for(int i=0;i<mDataList.size();i++){
				SelectItemBean itemBean = mDataList.get(i);
				if(itemBean.isChecked){
					
					selectMapList.add(itemBean.mSelectItemData);
				}
			}
		}
		return selectMapList;
	}
	
	@Override
	public View createContentView() {
		// TODO Auto-generated method stub
		View contentView = LayoutInflater.from(mContext).inflate(
				R.layout.app_widget_dialog_select, null);

		ListView listView = (ListView) contentView.findViewById(R.id.list);

		listView.setDivider(null);
		listView.setCacheColorHint(0x00000000);

		listView.setVerticalScrollBarEnabled(false);
		
		mAdapter = new DialogAdapter(mContext, mDataList);
		
		listView.setAdapter(mAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SelectItemBean b = (SelectItemBean) parent.getItemAtPosition(position);
				if(getSelectCount()<mMaxSelectItem||b.isChecked){
					
					if (b.isChecked) {
						b.isChecked = false;
					} else {
						b.isChecked = true;
					}
					mAdapter.notifyDataSetChanged();
				}else{
					CommonUtils.showAlertDialog(mContext, "无法选择更多");
				}
				
			}

		});
		setBtn1ClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mOnMultiSelectListener!=null){
					mOnMultiSelectListener.onMultiSelect(getSelectMapList());
				}
			}
		});
		setBtn2ClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mTempDataList!=null&&mTempDataList.size()>0&&mDataList!=null
						&&mDataList.size()==mTempDataList.size()){
					for(int i=0;i<mTempDataList.size();i++){
						SelectItemBean b = mDataList.get(i);
						if(mTempDataList.get(i)){
							b.isChecked = true;
						}else{
							b.isChecked = false;
						}
					}
					
				}
			}
		});
		return contentView;
	}

	
	/**
	 * 
	 * 选择完成回调接口
	 * 
	 * @author EX-LIJINHUA001
	 * @version [Android PABank C01, 2012-1-16]
	 */
	public interface OnMultiSelectListener {
		public void onMultiSelect(List<Map<String, Object>> selectItem);
	}
	
	public void setOnMultiSelectListener(OnMultiSelectListener listener){
		this.mOnMultiSelectListener = listener;
	}
	
	private class DialogAdapter extends BaseAdapter {
		/**
		 * LayoutInfalter
		 */
		private LayoutInflater inflater;
		/**
		 * 选项集合
		 */
		private List<SelectItemBean> list;
		/**
		 * 缓存对象
		 */
		ViewHolder holder;

		/**
		 * 
		 * [构造简要说明]
		 * 
		 * @param context
		 *            上下文
		 * @param list
		 *            集合
		 */
		public DialogAdapter(Context context, List<SelectItemBean> list) {
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			if (convertView == null||convertView.getTag()==null) {
				convertView = inflater.inflate(
						R.layout.app_widget_dialog_multiple_select_item, null);
				holder = new ViewHolder();
				holder.textView = (TextView) convertView
						.findViewById(R.id.multi_textView);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.multi_imageView);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			SelectItemBean bean = list.get(position);

			holder.textView.setText(bean.getShowTitle());
			if (bean.isChecked) {
				holder.imageView.setBackgroundResource(R.drawable.app_widget_radio_box_checked);
			} else {
				holder.imageView.setBackgroundResource(R.drawable.app_widget_radio_box);
			}
			return convertView;
		}

		class ViewHolder {
			TextView textView;
			ImageView imageView;
		}

	}
	
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		mTempDataList = new ArrayList<Boolean>();
		if(mDataList!=null&&mDataList.size()>0){
			for(int i=0;i<mDataList.size();i++){
				SelectItemBean b = mDataList.get(i);
				mTempDataList.add(b.isChecked);
			}
			
		}
	}


	/**
	 * 内部的bean类
	 * 用于值的传递
	 */
	private class SelectItemBean {
		private Map<String, Object> mSelectItemData;
		private boolean isChecked;
		public SelectItemBean(Map<String, Object> map){
			mSelectItemData = map;
			isChecked = false;
		}
		
		private String getShowTitle(){
			if(mShowKey!=null&&mSelectItemData!=null){
				Object ob = mSelectItemData.get(mShowKey);
				if(ob!=null){
					return ob.toString();
				}
			}
			return "";
		}
		
		
	}
}
